<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<?php
include_once 'global-includes.php';

	$messages = Messages::getInstance();	

	$cat_id = $_GET[category] ? $_GET[category] : null;
	$cat_name = $messages->getMessage('directory.links.category.all');
	if($cat_id){
		$sql = 'SELECT category FROM categories WHERE cid = ' . $cat_id;
		$res = BaseDbService::execute_select_query($sql);
		if($res){
			$cat_name = $res[0]['category']; 
		}
	}		
	$content  = "\t<title>$cat_name : "; 
	$content .= $messages->getMessage('directory.general.name') . "</title>\n";
	$content .= "\t<meta name=\"keywords\" content=\""; 
	$content .= $messages->getMessage('directory.general.keywords') . ", $cat_name\">\n";
	$content .= "\t<meta name=\"description\" content=\"";
	$content .= $messages->getMessage('directory.general.description') . ", $cat_name\">\n";
echo $content; 
?>
	<meta http-equiv="Content-Language" content="ru">
	<meta http-equiv="Content-Type" content="text/html; charset=utf8">
	<meta name="robots" content="all">
	<link href="links.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<center>
	<table class="level1" cellSpacing="0" cellPadding="0">
		<tr>
			<td colspan=2 class="headline"><h1><?php echo $messages->getMessage('directory.general.name');?></h1></td>
		</tr>
		<tr>
			<td colspan=2><hr size=1></td>
		</tr>
<?php 
$result = BaseDbService::execute_select_query("SELECT cid, category FROM categories");
for($i = 0; $i < count($result); $i++){
	$cid = $result[$i][cid];
	$cat = $result[$i][category];
 	$row_content  = "\t\t\t<td><strong><a href=\"" . $_SERVER[PHP_SELF] . '?category=' . $cid .'"';
 	$row_content .= ' title="' . $messages->getMessage('directory.links.category') . $cat .'">' . $cat . "</a></strong></td>\n";
	$row_content = ( $i % 2 == 0 ) ? "\t\t<tr>\n" . $row_content : $row_content . "\t\t</tr>\n";
	echo $row_content;
}
?>
		<tr>
			<td align=center colspan=2>
				<hr size=1>
				<font face="Verdana, Arial" size="2" color="black">
					<a href="add.php"><?php echo $messages->getMessage('directory.links.add'); ?></a> 
						<?php echo $messages->getMessage('directory.general.or'); ?>
						
					<a href="index.php"><?php echo $messages->getMessage('directory.links.category.show.all'); ?></a>
				</font>
				<hr size=1>
			</td>
		</tr>
		<tr>
			<td colspan=2>
				<font color="green" face="Verdana, Arial" size="4">
					<b><?php echo $messages->getMessage('directory.links.category') . ': ' . $cat_name ?></b>
				</font>
				<hr size=1>
			</td>
		</tr>
	</table>
</center>
<center>
	<table class="level1" cellspacing="0" cellpadding="3">
		<tr>
			<td colspan=2>
				<font face="Verdana, Arial" size="2" color="black">				
<?php
	$condition = $cat_id ? "cid = $cat_id" : null;
	$qnt = BaseDbService::get_count('base', $condition);								// items count
	
	// display the pages count 
	echo $messages->getMessage('directory.general.total') . ": $qnt || \n"; 
	$scrol = Scrollers::scroller ($_GET['page'],$qnt,$onpage,$page_rng,$cat_id);	
   	echo $scrol;
   	
?>
				</font>
				<hr size=1>
			</td>
		</tr>

<?php 
	
	
	$fst_item = (Scrollers::$page_cur - 1) * $onpage;										// first item number on current page
	//TODO: remove field 'category' from 'base' table!!!
	$sql  = "SELECT category, cid, htmltext, htmlimage FROM base";
	$sql .= $cat_id ? " WHERE cid=$cat_id" : '';
	$sql .= " ORDER BY date_create ASC LIMIT " . $fst_item . ", $onpage";
	 
	$record = BaseDbService::execute_select_query ($sql);
	
	$content = '';
	for($i = 0; $i < count($record); $i++){
		$html_img = $record[$i][htmlimage];
		$html_txt = $record[$i][htmltext];		
		$content .= "<tr>\n<td width=1 align=center valign=top>$html_img</td>\n";
		$content .= "<td valign=top><font face=\"Verdana, Arial\" size=\"2\" color=\"black\">$html_txt</font></td>\n</tr>\n";
		$content .= "<tr>\n<td colspan=2 align=right><font face=\"Verdana, Arial\" size=\"2\" color=\"black\"><i>";
		$content .= $messages->getMessage('directory.links.category') . ': ';
		$content .= "<a href=\"$_SERVER[PHP_SELF]?category=$cat_id\">$cat_name</a></i></font></td>\n</tr>\n";
		$content .= "<tr>\n<td colspan=2><hr size=1></td>\n</tr>\n";
	}
	echo $content;
?>		
		<tr>
			<td align="center" colspan="2">
				<font face="Verdana, Arial" size="2" color="black">
					<a href="add.php"><?php echo $messages->getMessage('directory.links.add'); ?></a> 
						<?php echo $messages->getMessage('directory.general.or'); ?>
						
					<a href="index.php"><?php echo $messages->getMessage('directory.links.category.show.all'); ?></a>
				</font>
			</td>
		</tr>
		<tr>
			<td align="center" colspan="2">
<?php 
//TODO: uncomment next line only for productional delivery
//include "counters.php";
?>
			</td>
		</tr>
	</table>
</center>	
</body>
</html>
